package servlet.community;

import domain.Community;
import org.junit.Test;
import service.community.CommunityService;
import service.community.CommunityServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ModifyCommunityServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        String newCommunityInfo = req.getParameter("updateCommunity");
        String communityId =  req.getParameter("Community_id");

        System.out.println("要修改的社区id："+communityId);
        System.out.println("要修改的社区信息："+newCommunityInfo);

        CommunityService communityService = new CommunityServiceImpl();
        if(newCommunityInfo == null){
            System.out.println("修改失败，填入的社区信息为空！");
            req.getRequestDispatcher("/admin/error/error404.jsp").forward(req,resp);
        } else{
            communityService.modifyCommunity(Integer.valueOf(communityId),newCommunityInfo);
            System.out.println("更新成功！");
            resp.sendRedirect("CommunityInfo");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

}
